<template>
	<view class="container">
		<view class="top">
			<image src="../../static/image/login/logo.png" mode=""></image>
		</view>
		<view class="content">
			<button class="btn1" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
				v-if="check">手机号授权登录</button>
			<button class="btn1" @click="popup" v-else>手机号授权登录</button>
			<button class="btn2">取消</button>
		</view>
		<view class="bottom">
			<checkbox-group>
				<checkbox class="check" v-bind:checked="check" @click="toggleCheckbox" />
			</checkbox-group>
			<text @click="handleCheckbox">我已阅读并同意</text>
			<text class="link" @click="privacy">《隐私政策》</text>
			<text>和</text>
			<text class="link" @click="user">《用户协议》</text>
		</view>
	</view>
</template>

<script>
	import $http from '@/ajax/http.js';
	export default {
		data() {
			return {
				check: false,
				phoneCode: ''
			}
		},

		created() {
			// 检查本地是否存在 token
			const token = wx.getStorageSync('token');

			if (token) {
				// 如果本地已有 token，直接跳转到主页或执行其他操作
				wx.switchTab({
					url: '/pages/index/index',
				});
			}
		},
		methods: {
			getPhoneNumber(e) {
				console.log(e.detail.code);
				this.phoneCode = e.detail.code

				if (!this.phoneCode) {
					wx.showModal({
						title: '授权失败',
						content: '您已拒绝获取绑定手机号登录授权',
						confirmColor: '#3C9CFFFF',
					});
					return false;
				}

				wx.login({
					success: res => {
						console.log(this.phoneCode, "-------------");
						console.log(res.code, "res.code");

						$http.request({
							url: '/storeuser/index/userLogin',
							method: 'POST',
							data: {
								loginCode: res.code,
								phoneCode: this.phoneCode
							},
						}).then((res) => {
							console.log(res, '请求结果');
							wx.setStorageSync('token', res.token);

							wx.setStorageSync("roles", res.userRole)

							wx.setStorageSync('shopToken', res.shopToken)
							wx.switchTab({
								url: '/pages/index/index',
							})
						})
					}
				})
			},

			toggleCheckbox() {
				this.check = !this.check;
			},
			handleCheckbox() {
				this.check = !this.check
				console.log(this.check, "this.check");
			},
			popup() {
				const _this = this; // 保存当前组件实例的引用

				wx.showModal({
					title: '隐私政策',
					content: '请选择我已阅读并同意《隐私政策》《用户协议》',
					success(res) {
						if (res.confirm) {
							// 用户点击了确定按钮
							_this.check = true; // 设置复选框的值为true，表示选中
						} else if (res.cancel) {
							// 用户点击了取消按钮
							_this.check = false; // 设置复选框的值为false，表示未选中
						}
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		display: flex;
		align-items: center;
		flex-direction: column;
		height: 100vh;
		background: linear-gradient(to top, RGBA(0, 74, 75, 1), RGBA(19, 149, 162, 1));

		.top {
			margin-top: 300rpx;
			width: 177rpx;
			height: 113rpx;

			image {
				width: 177rpx;
				height: 113rpx;
			}
		}

		.content {
			margin-top: 130rpx;

			button {
				width: 640rpx;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 32rpx;
				font-weight: 400;
				border-radius: 20rpx;
				box-shadow: 0 3px 6px rgba(0, 0, 0, 0.5);
			}

			.btn1 {
				color: #FFFFFF;
				background: linear-gradient(to right, RGBA(18, 178, 160, 1), RGBA(0, 129, 139, 1));
			}

			.btn2 {
				margin-top: 40rpx;
				color: #3E3A39;
			}


		}

		.bottom {
			display: flex;
			margin-bottom: 300rpx;
			font-size: 25rpx;
			font-family: MiSans;
			font-weight: 300;
			color: #FFFFFF;
			align-items: center;
			justify-content: center;
			margin-top: 100rpx;

			.check {
				transform: scale(0.5);
			}

			.link {
				color: #FFFFFF;
				text-decoration: none;
			}
		}
	}
</style>